from django.db.models import fields
from django.contrib.auth.models import User
from rest_framework import serializers

from .models import BlogArticle, Category, Tag

class CategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = Category
        fields = [
            'id',
            'category',
        ]

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = [
            'id',
            'username',
        ]

class TagSerializer(serializers.ModelSerializer):
    class Meta:
        model = Tag
        fields = [
            "id",
            "tag",
        ]

class BlogListSerializer(serializers.ModelSerializer):
    category = CategorySerializer()
    author = UserSerializer()
    tags = TagSerializer(many=True)
    
    class Meta:
        model = BlogArticle
        fields = [
            'id',
            'title',
            'createTime',
            'author',
            'category',
            'tags',
            'views',
        ]

class BlogRetrieveSerializer(serializers.ModelSerializer):
    category = CategorySerializer()
    author = UserSerializer()
    tags = TagSerializer(many=True)
    
    class Meta:
        model = BlogArticle
        fields = [
            'id',
            'title',
            'createTime',
            'updateTime',
            'author',
            'category',
            'tags',
            'views',
            'body',
        ]
